package com.atguigu.tingshu.user.pattern.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.model.user.UserVipService;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.user.client.UserFeignClient;
import com.atguigu.tingshu.user.mapper.UserInfoMapper;
import com.atguigu.tingshu.user.mapper.UserVipServiceMapper;
import com.atguigu.tingshu.user.mapper.VipServiceConfigMapper;
import com.atguigu.tingshu.user.pattern.DeliveryStrategy;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * ClassName : VIPDelivery
 * Package : com.atguigu.tingshu.user.pattern.impl
 * Description :
 * <p>
 * Author: LHZ
 * Create: 2025/8/1 23:51
 * Version: 1.0
 */
@Slf4j
@SuppressWarnings({"all"})
@Component(SystemConstant.ORDER_ITEM_TYPE_VIP)
public class VIPDelivery implements DeliveryStrategy {

    @Autowired
    private UserVipServiceMapper userVipServiceMapper;
    @Autowired
    private VipServiceConfigMapper vipServiceConfigMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void delivery(UserPaidRecordVo vo) {
        //获取订单号
        String orderNo = vo.getOrderNo();
        //VIP
        //根据订单号从用户购买记录中进行查询是否有此记录
        Long count = userVipServiceMapper.selectCount(
                new LambdaQueryWrapper<UserVipService>()
                        .eq(UserVipService::getOrderNo, orderNo)
        );
        if(count > 0){
            log.info("已存在该订单:{}的已购VIP记录", orderNo);
            return;
        }
        //判断当前用户是否是会员
        UserInfo userInfo = userInfoMapper.selectById(vo.getUserId());
        //远程调用的方式获取userInfoVo得到用户信息
//        UserInfoVo userInfoVo = userFeignClient.getUserInfoVo(vo.getUserId()).getData();
//        Assert.notNull(userInfoVo,"获取到的用户:{}信息为空",vo.getUserId());
        Boolean isVIP = false;
        if(userInfo.getIsVip().intValue()==1 && userInfo.getVipExpireTime().after(new Date())){
            isVIP = true;
        }
        //获取用户购买的套餐
        VipServiceConfig vipServiceConfig = vipServiceConfigMapper.selectById(vo.getItemIdList().get(0));
        //获取套餐的服务月数
        Integer serviceMonth = vipServiceConfig.getServiceMonth();
        //计算vip到期时间
        Date startTime;
        Date expireTime;
        if(isVIP){
            //如果本身就是会员
            startTime = DateUtil.offsetDay(userInfo.getVipExpireTime(),1);
            expireTime = DateUtil.offsetMonth(startTime,serviceMonth);
        }else{
            //如果本来不是会员
            startTime = new Date();
            expireTime = DateUtil.offsetMonth(new Date(), serviceMonth);
        }
        //如果不存在此记录则声明对象并存入数据库中
        UserVipService userVipService = new UserVipService();
        userVipService.setOrderNo(orderNo);
        userVipService.setUserId(vo.getUserId());
        userVipService.setStartTime(startTime);
        userVipService.setExpireTime(expireTime);
//			userVipService.setIsAutoRenew();
//			userVipService.setNextRenewTime();
        userVipServiceMapper.insert(userVipService);
        //更新用户信息
        userInfo.setIsVip(1);
        userInfo.setVipExpireTime(expireTime);
//        UserInfo userInfo = BeanUtil.copyProperties(userInfoVo, UserInfo.class);
        userInfoMapper.updateById(userInfo);
    }
}